import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/process_nodes_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/risk_control/equipment_start_shutdown/equipment_start_shutdown_controller.dart';

/// 装置开停车大检修台账（审批）
class EquipmentStartShutdownApproveController extends GetxController with GetSingleTickerProviderStateMixin {
  /// 我的任务进入（是否显示流程审批底部按钮）
  bool isMeTask = Get.arguments['isMeTask'] as bool;
  String id = Get.arguments['id'] as String;

  /// 审核意见
  String approvalOpinions = '';

  /// 签字
  String approvalUser = '';

  /// TabController 变量
  late TabController tabController;

  /// 审批流程数据
  List<ProcessNodesEntity>? processList;

  @override
  void onInit() {
    super.onInit();
    //初始化tabController
    tabController = TabController(vsync: this, length: 2);
    getProcessListData();
  }

  /// 获取流程列表数据
  void getProcessListData() async {
    // 获取流程列表接口
    final res = await HttpUtil().generateGetRequest<List<ProcessNodesEntity>>(
      Interface.getApprovalHistory,
      queryParameters: {'businessId': Get.arguments['id']},
      // queryParameters: {'businessId': '4fd77c39-8617-4cc0-97eb-558bcbda814c'},
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        processList = result.data!;
      }
      update();
    });
  }

  /// 输入回调
  void onOpinion(String value) {
    approvalOpinions = value;
    update();
  }

  /// 签字回调
  void onSign(String url) {
    approvalUser = url;
  }

  /// 操作回调
  /// [type]1:通过 2:驳回 驳回即为不通过
  Future<void> onOperation(int type) async {
    // 添加分配（为部门分配任务）
    final res = await HttpUtil().generatePostRequest(
      Interface.startApproval,
      data: {
        'businessId': id, //业务数据id
        'reject': type == 1 ? false : true, //是否驳回
        'comments': approvalOpinions, //	意见
        'code': 2, //	页面编号：1.安全风险辨识管理 2.装置开停车大检查台账 3.年度隐患排查计划审核
        'signatureAddress': approvalUser //签名地址
      },
    ).execute();
    res.onSuccess((result) {
      Get.back(closeOverlays: true);
      // 刷新列表
      final EquipmentStartShutdownController equipmentStartShutdownController = Get.find();
      equipmentStartShutdownController.globalKey.currentState?.onRefresh();
    });
  }
}
